package com.zyb.eduservice.controller;


import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.zyb.commonutils.R;
import com.zyb.eduservice.client.VideoClient;
import com.zyb.eduservice.entity.EduVideo;
import com.zyb.eduservice.service.EduVideoService;
import com.zyb.servicebase.exception.GuliException;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author testJava
 * @since 2022-04-02
 */
@RestController
@RequestMapping("/eduservice/video")
//@CrossOrigin
public class EduVideoController {

    @Resource
    private EduVideoService videoService;
    @Resource
    private VideoClient videoClient;

    //添加小节
    @PostMapping("addVideo")
    public R addVideo(@RequestBody EduVideo eduVideo) {
        videoService.save(eduVideo);
        return R.ok();
    }

    //删除小节删除小节的同时把视频删掉
    @DeleteMapping("{videoId}")
    public R deleteVideo(@PathVariable("videoId") String videoId) {
        //根据小节id获取视频id，调用方法实现视频删除
        EduVideo eduVideo = videoService.getById(videoId);
        String videoSourceId = eduVideo.getVideoSourceId();
        //判断小节里面是否有视频id
        if (!StringUtils.isEmpty(videoSourceId)) {
            //根据视频id，远程调用实现视频删除
            R result = videoClient.removeAlyVideo(videoSourceId);
            if (result.getCode() == 20001) {
                throw new GuliException(20001, "删除视频失败，熔断器...");
            }
        }
        //删除小节
        videoService.removeById(videoId);
        return R.ok();
    }

    //根据videoInfo查询一条videoInfo信息
    @GetMapping("getVideoInfoById/{videoId}")
    public R getVideoInfoById(@PathVariable String videoId) {
        EduVideo videoInfo = videoService.getById(videoId);
        return R.ok().data("videoInfo", videoInfo);
    }

    //修改小节
    @PostMapping("updateVideo")
    public R updateVideo(@RequestBody EduVideo eduVideo) {
        videoService.updateById(eduVideo);
        return R.ok();
    }
}

